/*
 * Created on Mar 24, 2005
 * Created by dfhuynh
 */
package edu.mit.simile.longwell.corpus;

import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Properties;

import org.apache.log4j.Logger;

import tdb.cmdline.ModTDBAssembler;

import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ModelMaker;
import com.hp.hpl.jena.tdb.TDBFactory;
import com.hp.hpl.jena.tdb.assembler.TDBGraphAssembler;

import fr.inria.jfresnel.fsl.FSLHierarchyStore;
import fr.inria.jfresnel.fsl.FSLNSResolver;
import fr.inria.jfresnel.fsl.jena.FSLJenaEvaluator;


/**
 * @author dfhuynh
 */
public class TDBConnector implements Connector {
	/* (non-Javadoc)
	 * @see edu.mit.simile.corpus.IJdbcConnector#connect(java.util.Properties, java.lang.String)
	 */
	
	
	public Corpus connect(Properties properties, String corpusURI) {
		return new TDBCorpus(
			corpusURI, 
			properties.getProperty("database"),
			properties.getProperty("modelName"),
			properties.getProperty("TDBAssembler"),
			properties.getProperty("login")
		);
	}

}

class TDBCorpus extends Corpus {
	static private Logger s_logger = Logger.getLogger(TDBCorpus.class); 
	final protected File  m_TDBAssembler;
	final protected String	m_database;
	final protected String	m_modelName;
	final protected String	m_login;

	protected ModelMaker	m_modelMaker;
	protected Connection	m_connection;

	public TDBCorpus(String corpusURI, String database, String modelName, String TDBAssembler, String login) {
		super(corpusURI);
		m_database = database;
		m_modelName = modelName;
		m_TDBAssembler=new File(TDBAssembler);
		m_login = login;
	}

	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.Corpus#dispose()
	 */
	public void dispose() {
		if (m_initialized) {
			if (m_modelMaker != null) {
				m_modelMaker.close();
				m_modelMaker = null;
			}
			if (m_connection != null) {
				try {
					m_connection.close();
				} catch (SQLException e) {
					s_logger.warn(
						"Error closing HSQLDB connection",
						e
					);
				}
				m_connection = null;
			}

			super.dispose();
		}
	}

	protected void createModel() {
		System.out.println("CALL createModel in TDBConnector");
		if (m_model != null) {
			return;
		}
		
		//String dir =m_TDBAssembler.getPath();
		//File[] files = m_TDBAssembler.listFiles();
		//Dataset ds = null;
		m_model = ModelFactory.createDefaultModel();
//		
//		for (int i = 0; i < files.length; i++) {
//			File file = files[i];
//			String assemblerFile = file.getName() ;    
//			s_logger.info("Loading assembler: "+assemblerFile);
//			ds = TDBFactory.assembleDataset(dir+"/"+assemblerFile);		
//			/*
//			Iterator<String> graphNames = ds.listNames();
//			while (graphNames.hasNext()) {
//			    String graphName = graphNames.next();
//			    //m_model.add(ds.getNamedModel(graphName));
//			}
//			*/
//		}
		
		//String service = "http://192.168.234.97:2020/myDataset/query";
	}
}
